<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :model="queryForm" inline>
        <el-form-item
          :label="sysBusinessProductStandard.productStandardName"
          prop="productStandardName"
        >
          <el-input
            v-model="queryForm.productStandardName"
            :placeholder="sysBusinessProductStandard.productStandardName"
            clearable
            @keyup.enter.native="getList()"
          />
        </el-form-item>
        <el-form-item class="base-filter-button">
          <el-button
            v-loading="searchLoading"
            type="primary"
            @click="onQuery"
            >{{ operateType.query }}</el-button
          >
          <el-button @click="cancelForm">{{ operateType.clear }}</el-button>
        </el-form-item>
      </el-form>
      <!--操作栏-->
      <div class="base-content-operate">
        <el-button
          v-rights="'Add'"
          icon="el-icon-plus"
          type="primary"
          @click="onNew"
          >{{ operateType.add }}</el-button
        >
        <el-button
          v-rights="'Import'"
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >{{ operateType.import }}</el-button
        >
        <!--
          <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >导入</el-button>

          <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >导出</el-button> -->
      </div>
      <el-table
        :data="list"
        tooltip-effect="dark"
        height="calc(100% - 150px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          :label="sysBusinessProductStandard.serialNumber"
          width="50"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="产品标准编码">
          <template slot-scope="scope">
            {{ scope.row.productStandardNum }}
          </template>
        </el-table-column> -->
        <el-table-column
          :label="sysBusinessProductStandard.productStandardName"
        >
          <template slot-scope="scope">
            {{ scope.row.productStandardName }}
          </template>
        </el-table-column>
        <el-table-column
          :label="sysBusinessProductStandard.productStandardNumber"
        >
          <template slot-scope="scope">
            {{ scope.row.nationalStandardNum }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessProductStandard.tableNumber">
          <template slot-scope="scope">
            {{ scope.row.tableNum }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessProductStandard.revocatoryDate">
          <template slot-scope="scope">
            {{ formatTime(scope.row.revocatoryDate) }}
          </template>
        </el-table-column>
        <el-table-column :label="sysBusinessProductStandard.lastEditingTime">
          <template slot-scope="scope">
            {{ formatTime(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column :label="operateType.action">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                v-rights="'Edit'"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ operateType.redact }}</el-link
              >
              <el-link
                v-rights="'Delete'"
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >{{ operateType.delete }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
    </div>
    <BusinessImportData
      v-if="dialogVisible"
      ref="BusinessImportData"
      :dialog-visible.sync="dialogVisible"
      :sys-business-import-type.sync="sysBusinessImportType"
      @load="getList"
    />

    <SysBusinessProductStandardEdit
      ref="SysBusinessProductStandardEdit"
      :dialog-form-visible.sync="dialogFormVisible"
    />
  </div>
</template>

<script>
import { formatTime } from "../../../../utils/time";
import {
  deleteRecord,
  getList,
} from "../../../../api/DetectionItemManage/SysBusinessProductStandard.js";
import BusinessImportData from "../../../../components/SysBusinessImportDialog.vue";
import SysBusinessProductStandardEdit from "./SysBusinessProductStandardEdit.vue";
export default {
  name: "SysBusinessProductStandardList",
  components: { SysBusinessProductStandardEdit, BusinessImportData },
  data() {
    return {
      sysBusinessImportType: "2",
      dialogVisible: false,
      loading: true,
      list: [],
      queryForm: {
        productStandardName: "",
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      searchLoading: false,
    };
  },
  computed: {
    operateType() {
      return this.$t("ModuleSysBusiness.OperateType") || {};
    },
    sysBusinessProductStandard() {
      return this.$t("SysBusinessProductStandard") || {};
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 导入
    importData() {
      this.dialogVisible = true;
    },
    formatTime,
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.$refs.SysBusinessProductStandardEdit.formData = this.Helper.deepCopy(
        this.$refs.SysBusinessProductStandardEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.$refs.SysBusinessProductStandardEdit.formData = JSON.parse(
        JSON.stringify(rowData)
      );
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
    // 清空筛选条件
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },

    // 选择列表
    handleSelectionChange(row) {
      this.selectedIds = row.length ? row.map((v) => v.id) : [];
    },
  },
};
</script>
